package com.wephone.Student.ui;

import java.io.File;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import com.loopj.android.http.RequestParams;
import com.umeng.fb.FeedbackAgent;
import com.wephone.Student.BaseApplication;
import com.wephone.Student.R;
import com.wephone.Student.bean.BaseResponse;
import com.wephone.Student.bean.CheckVersionResponse;
import com.wephone.Student.network.HttpClient;
import com.wephone.Student.network.MyResponseHandler;
import com.wephone.Student.util.Constants;
import com.wephone.Student.util.ShareReferenceUtil;

public class Settings2Activity extends BaseActivity {
	private FeedbackAgent agent;

	private MyResponseHandler getHandler(){
		MyResponseHandler handler = new MyResponseHandler(Settings2Activity.this,CheckVersionResponse.class){
			@Override
			public void onSuccess(String result) {
				super.onSuccess(result);
				
				if(getDataSuccess){
					try {
						final CheckVersionResponse checkResponse = (CheckVersionResponse)response;
						PackageManager pm = getPackageManager();
						PackageInfo pi = pm.getPackageInfo(getPackageName(), 0);
						int currVersion = pi.versionCode;
						float serviceVersion = Float.parseFloat(checkResponse.getVersion())*10;
						String versionMsg = checkResponse.getInfo().replace("\\n", "\n");
//						System.out.println("---versionMsg---"+versionMsg);
						if(serviceVersion>currVersion){
							//TODO
							new AlertDialog.Builder(Settings2Activity.this)
							.setTitle("发现新版本")
							.setMessage(versionMsg)
							.setPositiveButton("确定", null).show();
						}else{
							Toast.makeText(getApplicationContext(), R.string.settings_item_title_6_no_new_version, Toast.LENGTH_SHORT).show();
						}
					} catch (Exception e) {
						e.printStackTrace();
					}
					
				}
			}
		};
		return handler;
	}
	
	private MyResponseHandler getHandler2(){
		MyResponseHandler handler = new MyResponseHandler(Settings2Activity.this,BaseResponse.class){
			@Override
			public void onSuccess(String result) {
				super.onSuccess(result);
			}
		};
		return handler;
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		if(requestCode==11 && resultCode==11){
			MainTabBar.needRefresh = true;
		}
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_settings2);
		
		init();
		
		agent = new FeedbackAgent(Settings2Activity.this);
		agent.sync();
	}
	
	private void init(){
		findViewById(R.id.btn_back).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg0) {
				finish();
			}
		});
		findViewById(R.id.btn_top_right).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				new AlertDialog.Builder(Settings2Activity.this)
				.setTitle("提示")
				.setMessage("确定退出登录吗？")
				.setNegativeButton("取消", new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
					}
				})
				.setPositiveButton("确定", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int whichButton) {
						ShareReferenceUtil.saveUserInfoJson(getApplicationContext(), "");
						//reg push
						RequestParams params = new RequestParams();
						params.put("uId", BaseActivity.getMyInfo(getApplicationContext()).getuId());
						params.put("uPushIdentifier", "0");
						HttpClient.getInstance().get(Constants.API_REG_PUSH_URL, params, getHandler2());
						
						BaseApplication.getInstance().logout();
						ShareReferenceUtil.savePushUserInfoJson(getApplicationContext(), null);
						
						setResult(22);
						finish();
					}
				}).show();
			}
		});
		findViewById(R.id.settings_item_layout_1).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				startActivityForResult(new Intent(getApplicationContext(),EditMyInfoActivity.class),11);
			}
		});
		findViewById(R.id.settings_item_layout_2).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				startActivity(new Intent(getApplicationContext(),MySignatureActivity.class));
			}
		});
		findViewById(R.id.settings_item_layout_3).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
//				startActivity(new Intent(getApplicationContext(),SuggestionsActivity.class));
			    agent.startFeedbackActivity();
			}
		});
		findViewById(R.id.settings_item_layout_4).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				
			}
		});
		findViewById(R.id.settings_item_layout_5).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				startActivity(new Intent(getApplicationContext(),AboutUsActivity.class));
			}
		});
		findViewById(R.id.settings_item_layout_6).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				RequestParams params = new RequestParams();
				HttpClient.getInstance().post(Constants.API_CHECK_APK_NEW_VERSION_URL, params, getHandler());
			}
		});
		findViewById(R.id.settings_item_layout_7).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				deleteAllSubFiles();
				Toast.makeText(getApplicationContext(), R.string.settings_item_title_7_clear_success, Toast.LENGTH_SHORT).show();
			}
		});
		
	}

	private void deleteAllSubFiles(){
		File dFile = new File(Constants.IMAGE_CACHE_SAVE_PATH);
		if(dFile.exists()){
			File[] subFiles = dFile.listFiles();
			if(subFiles!=null && subFiles.length>0){
				for(int i=0;i<subFiles.length;i++){
					subFiles[i].delete();
				}
			}
		}
		imageLoader.clearDiscCache();
	}
	
}
